<?php
namespace App\Admin\Controller;

use Illuminate\Routing\Controller;
use ModStart\Admin\Concern\HasAdminCRUD;
use Illuminate\Support\Facades\DB;
use ModStart\Core\Exception\BizException;
use ModStart\Core\Input\Response;
use ModStart\Detail\Detail;
use ModStart\Form\Form;
use ModStart\Grid\Grid;

class ExchangerateController extends Controller
{
    use HasAdminCRUD;
    public function grid()
    {
        $grid = Grid::make('exchangerates');
        $grid->id('id', 'ID');
       
        $grid->select('blog_id', '货币名称')->options(['1' => '人民币','2' => '龙币']);
        //$grid->text('ex_blog_id', '兑换货币');
        $grid->select('ex_blog_id', '兑换货币名称')->optionArray($this->getBlogList(),'id','name');
        $grid->text('ex_rates', '兑换率');
//        $grid->image('icon', '图标');
//        $grid->text('summary', '兑换比例')->placeholder("1瓦努阿图币=***");
//        $grid->text('content', '适用国家');
        $grid->display('created_at', '创建时间');
        $this->pageTitle('汇率管理');
        return $grid;
    }
    public function form()
    {

        $form = Form::make('exchangerates');
         $form->select('blog_id',"货币名称")->options(['1' => '人民币','2' => '龙币'])->required();
         $form->select('ex_blog_id',"兑换货币")->optionArray($this->getBlogList(),'id','name')->required();
        $form->text('ex_rates', '汇率')->required();
        $form->hookSaving(function (Form $form) {
            if ($form->isModeAdd()) {
                // 增加模式
                $data = $form->dataAdding();
                if($data['blog_id'] ==1 && $data['ex_blog_id'] != 2){
                    return Response::generate(-1, '人民币只能对换龙币');
                }
                if ($data['blog_id'] == $data['ex_blog_id']) {
                    return Response::generate(-1, '同种货币之间不能兑换');
                }
                $exch = DB::table("exchangerates")->where(['blog_id' => $data['blog_id'], 'ex_blog_id' => $data['ex_blog_id']])->first();
                if ($exch) {
                    return Response::generate(-1, '该汇率记录已存在,不能重复提交');
                }
                // 替换入库数据
                $form->dataAdding($data);
            } else if ($form->isModeEdit()) {
                // 修改
                $data = $form->dataEditing();
                $id = $form->item()->id;
                if($data['blog_id'] ==1 && $data['ex_blog_id'] != 2){
                    return Response::generate(-1, '人民币只能对换龙币');
                }
                if ($data['blog_id'] == $data['ex_blog_id']) {
                    return Response::generate(-1, '同种货币之间不能兑换');
                }
                $exch = DB::table("exchangerates")->where(['blog_id' => $data['blog_id'], 'ex_blog_id' => $data['ex_blog_id']])->where("id","!=",$id)->first();
                if ($exch) {
                    return Response::generate(-1, '该汇率记录已存在,不能重复提交');
                }
                // 替换入库数据
                $form->dataEditing($data);
            }
        });
        return $form;
    }
    public function detail()
    {
        $detail = Detail::make('exchangerates');
        $detail->id('id', 'ID');
        $detail->display('created_at', '创建时间');
        $detail->text('title', '货币名称');
        $detail->image('cover', '封面图');
        $detail->image('icon', '图标');
        $detail->text('summary', '兑换比例')->placeholder("1瓦努阿图币=***");
        $detail->text('content', '适用国家');
        return $detail;
    }
    
      public function getBlogList(){
//       $res =  DB::table("blog")->where("id","!=",2)->select("id","title as text")->get();
       $res =  DB::table("blog")->where("state","=",1)->select("id","title as text")->get();
       $lit = [];
       foreach($res as $k => $v){
           $lit[$k]['id'] = $v->id;
           $lit[$k]['name'] = $v->text;
//          $lit[] = $v->text;
       }
       return $lit;
    }
}